﻿

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoLocation
    {
        public daoLocation()
        {

        }
        private Location Load(IDataReader de)
        {
            Location item = new Location();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.LocationId = clsConvert.DbToInt("LocationId", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.ModifiedBy = clsConvert.DbToString("ModifiedBy", de);
            item.ModifiedDate = clsConvert.DbToDateTime("ModifiedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }



        public Location GetByID(int locationId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Location item = new Location();

            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = locationId;
                using (de = clsSQL.SPDataReader("Location_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoLocation", "GetByID(int locationId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }

        public LocationCollection GetAll()
        {
            clsSQL clsSQL = new clsSQL();
            SqlDataReader de;
            LocationCollection items = new LocationCollection();
            try
            {
                de = clsSQL.SPDataReader("Location_GetAll");
                while (de.Read())
                {
                    Location item = Load(de);
                    items.Add(item);
                }
                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoLocation", "GetAll()", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
        public LocationCollection List()
        {
            clsSQL clsSQL = new clsSQL();
            SqlDataReader de;
            LocationCollection items = new LocationCollection();
            try
            {
                de = clsSQL.SPDataReader("Location_List");
                while (de.Read())
                {
                    Location item = Load(de);
                    items.Add(item);
                }

                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoLocation", "List", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

        public bool OrderUp(int LocationId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = LocationId;
                result = clsSQL.SPExecute("Location_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "OrderUp(int LocationId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderDown(int LocationId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = LocationId;
                result = clsSQL.SPExecute("Location_OrderDown", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "OrderDown(int LocationId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Active(int location)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = location;
                result = clsSQL.SPExecute("Location_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "Active(int location)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool DeActive(int Location)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = Location;
                result = clsSQL.SPExecute("Location_DeActive", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "DeActive(int location)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int LocationId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[0].Value = LocationId;
                result = clsSQL.SPExecute("Location_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "Delete(int locationId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Insert(Location objLocation)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[4];
            try
            {
                para[0] = new SqlParameter(busLocation.Name, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objLocation.Name);
                para[1] = new SqlParameter(busLocation.IsActive, SqlDbType.Bit);
                para[1].Value = objLocation.IsActive;
                para[2] = new SqlParameter(busLocation.CreatedBy, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objLocation.CreatedBy);
                para[3] = new SqlParameter(busLocation.CreatedDate, SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objLocation.CreatedDate);              
                result = clsSQL.SPExecute("Location_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "Insert(Location objLocation)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Update(Location objLocation)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[5];
            try
            {
                para[0] = new SqlParameter(busLocation.Name, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objLocation.Name);
                para[1] = new SqlParameter(busLocation.IsActive, SqlDbType.Bit);
                para[1].Value = objLocation.IsActive;
                para[2] = new SqlParameter(busLocation.ModifiedBy, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objLocation.ModifiedBy);
                para[3] = new SqlParameter(busLocation.ModifiedDate, SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objLocation.ModifiedDate);
                para[4] = new SqlParameter(busLocation.LocationId, SqlDbType.Int);
                para[4].Value = objLocation.LocationId;
                result = clsSQL.SPExecute("Location_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLocation", "Update(Location objLocation)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

    }
}
